/** @noSelfInFile **/

import {HandleModel} from "../model1/HandleModel";
import {RectModel} from "../model2/RectModel";
import {PlayerModel} from "./PlayerModel";
import {UnitModel} from "../model3/UnitModel";
import {GlobalModel} from "../model1/GlobalModel";
import {
    ITriggerAdapter, MouseEventInfo, PlayerEvent,
    PlayerUnitEvent,
    triggetAdapter,
    UnitEvent
} from "../../adapter/adapterInterface/model4/ITriggerAdapter";


export class TriggerModel implements HandleModel {
    public handle: any;
    id: string;
    nikeName: string;

    triggerAdapter: ITriggerAdapter;

    constructor(name: string) {
        this.nikeName = name;
        this.triggerAdapter = triggetAdapter.adapter;

        const handle = this.triggerAdapter.createHandle();
        this.bindHandle(handle)
    }

    /**
     * 添加触发器动作
     */
    public addAction(actionFunc: () => void) {
        return this.triggerAdapter.addAction(this.handle, actionFunc);
    }


    public registerMouseWheel(callBack: (this: void, bStop: boolean) => void, sync = false) {
        this.triggerAdapter.registerMouseWheel(this.handle, callBack, sync);
    }

    public registerUpdate(callBack: () => void) {
        this.triggerAdapter.registerUpdate(this.handle, callBack);
    }

    /**
     * 任意玩家单位事件
     */
    public registerAnyUnitEvent(whichPlayerUnitEvent: PlayerUnitEvent) {
        this.triggerAdapter.registerAnyUnitEvent(this.handle, whichPlayerUnitEvent);
    }

    public registerCommandEvent(whichAbility: number, order: string) {
        this.triggerAdapter.registerCommandEvent(this.handle, whichAbility, order);
    }

    public registerWindowResizeEvent(sync: boolean, fun: (this: void) => void) {
        this.triggerAdapter.registerWindowResizeEvent(this.handle, sync, fun);
    }

    /**
     * 单位进入区域[R]
     */
    public registerEnterRect(whichRect: RectModel) {
        this.triggerAdapter.registerEnterRect(this.handle, whichRect.handle);
    }

    /**
     * 单位离开区域[R]
     */
    public registerLeaveRect(whichRect: RectModel) {
        this.triggerAdapter.registerLeaveRect(this.handle, whichRect.handle);
    }

    /**
     * 任意玩家输入聊天信息
     */
    public registerAnyPlayerChatEvent(players: PlayerModel[], chatMessageToDetect: string, exactMatchOnly: boolean) {
        this.triggerAdapter.registerAnyPlayerChatEvent(this.handle, players, chatMessageToDetect, exactMatchOnly);
    }

    /**
     * 任意玩家事件
     */
    public registerAnyPlayerEvent(players: PlayerModel[], playerEvent: PlayerEvent) {
        this.triggerAdapter.registerAnyPlayerEvent(this.handle, players, playerEvent);
    }

    public registerPlayerSelected(players: PlayerModel[], selected: boolean) {
        this.triggerAdapter.registerPlayerSelected(this.handle, players, selected);
    }

    public registerKeyEvent(key: number, state: number, sync: boolean, fun: () => void) {
        this.triggerAdapter.registerKeyEvent(this.handle, key, state, sync, fun);
    }

    public registerMouseEvent(sync: boolean, fun: (this: void, mouseEventInfo: MouseEventInfo) => void) {
        this.triggerAdapter.registerMouseEvent(this.handle, sync, fun);
    }

    /**
     * 详细单位的事件
     */
    public registerUnitEvent(whichUnit: UnitModel, whichEvent: UnitEvent) {
        this.triggerAdapter.registerUnitEvent(this.handle, whichUnit, whichEvent);
    }

    public registerSyncData(key: string) {
        this.triggerAdapter.registerSyncData(this.handle, key);
    }

    /**
     * 重置触发
     */
    public reset() {
        this.triggerAdapter.reset(this.handle);
    }

    /**
     * 删除触发器 [R]
     */

    bindHandle(handle: any) {
        this.triggerAdapter.bindHandle(handle);
        this.handle = handle;
        this.id = GlobalModel.getHandleId(this.handle);
    }

    equal(handle: TriggerModel): boolean {
        return this.triggerAdapter.equal(this.handle, handle.handle);
    }

    getHandleId(): string {
        return this.id;
    }

    isValid(): boolean {
        return this.triggerAdapter.isValid(this.handle);
    }

    public destroy() {
        this.triggerAdapter.destroy(this.handle)
        this.handle = undefined;
    }
}
